/**
 * 
 */
package jnzb.ui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import jnzb.control.Controller;
import jnzb.control.queue.QueueChangeListener;
import jnzb.control.queue.QueueManager;
import jnzb.ui.queue.FinishedPanel;
import jnzb.ui.queue.QueueControlPanel;
import jnzb.ui.queue.QueueFrame;

/**
 * Information about the NZB being processed as far as the queue of nzb is
 * concerned
 * 
 * @author Rid
 * 
 */
@SuppressWarnings("serial")
public class QueuePanel extends JPanel implements QueueChangeListener {
	private static final String QUEUE_EMPTY_TEXT = "NZB queue is empty";

	private JLabel infoField = new JLabel(QUEUE_EMPTY_TEXT);
	private JButton showAll = new JButton("Show Queue");

	private QueueFrame queueControlFrame = new QueueFrame();

	private QueueManager queueManager;

	public QueuePanel() {
		create();
	}

	private void create() {
		Border etchedBorder = BorderFactory
				.createEtchedBorder(EtchedBorder.LOWERED);
		Border titleBorder = BorderFactory.createTitledBorder(etchedBorder,
				"NZB Queue");
		this.setBorder(titleBorder);

		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

		Font boldFont = new Font(super.getFont().getName(), Font.BOLD, super
				.getFont().getSize());

		JLabel info = new JLabel("Queue");
		info.setFont(boldFont);
		info.setMaximumSize(info.getPreferredSize());

		infoField.setHorizontalTextPosition(JLabel.LEFT);
		infoField.setHorizontalAlignment(JLabel.LEFT);

		showAll.setMaximumSize(showAll.getPreferredSize());

		this.add(Box.createHorizontalStrut(10));
		this.add(info);
		this.add(Box.createHorizontalStrut(5));
		this.add(infoField);
		this.add(Box.createHorizontalGlue());
		this.add(showAll);
		this.add(Box.createHorizontalStrut(10));

		int h = this.getPreferredSize().height;

		// Set max size to the preferred size so it'll only resize horizontally
		this.setMaximumSize(new Dimension(9999, h));

	}

	public void registerListeners(Controller controller) {
		this.showAll.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent event) {
				queueControlFrame.setVisible(true);
			}
		});
			
		this.queueControlFrame.registerListeners(controller);
	}

	/**
	 * Pass in the manager for the queue model.
	 * @param queueManager
	 */
	public void setQueueManager(QueueManager queueManager) {
		queueManager.registerListener(this);
		this.queueManager = queueManager;
	}

	public QueueControlPanel getWaitingQueuePanel() {
		return queueControlFrame.getQueueControlPanel();
	}

	public FinishedPanel getFinishedPanel() {
		return queueControlFrame.getFinishedPanel();
	}

	/**
	 * Receive indication that the queue has changed and updates the display
	 * @see jnzb.control.queue.QueueChangeListener#queueChanged()
	 */
	@Override
	public void queueChanged() {
		final String latestInfo;
		if (queueManager != null && !queueManager.isQueueEmpty())
			latestInfo = "Number of NZBs in the queue (including current) : "
					+ queueManager.getQueueSize();
		else
			latestInfo = QUEUE_EMPTY_TEXT;

		Runnable r = new Runnable() {
			public void run() {
				infoField.setText(latestInfo);
				infoField.setToolTipText(latestInfo);
			}
		};
		SwingUtilities.invokeLater(r);
	}
	
	/**
	 * Hide the queue control frame
	 */
	public void hideQueueControlFrame(){
		this.queueControlFrame.setVisible(false);
	}
}
